﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class UIVAK : ViewBase
    {
        public VVAKOLL TMV
        {
            get
            {
                return (VVAKOLL)MV;
            }
        }
        #region ==dwVAK===========
        public static readonly DependencyProperty dwVAKProperty = DependencyProperty.Register("dwVAK",
            typeof(DataRow), typeof(UIVAK),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwVAKChanged));
        public DataRow dwVAK
        {
            get
            {
                return (DataRow)GetValue(dwVAKProperty);
            }
            set
            {
                SetValue(dwVAKProperty, value);
            }
        }
        private static void OndwVAKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVAK;
            if (src != null)
            {
                src.dwVAK = (DataRow)e.NewValue;
            }
        }
        #endregion
        #region ==KM1===========
        public static readonly DependencyProperty KM1Property = DependencyProperty.Register("KM1",
            typeof(string), typeof(UIVAK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKM1Changed));
        public string KM1
        {
            get
            {
                return (string)GetValue(KM1Property);
            }
            set
            {
                SetValue(KM1Property, value);
                if (MV == null) return;
                ((VVAKOLL)MV).KM1 = value;
            }
        }
        private static void OnKM1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVAK;
            if (src != null)
            {
                src.KM1 = (string)e.NewValue;
            }
        }
        #endregion

        #region ==IsKOLLManage===========
        public static readonly DependencyProperty IsKOLLManageProperty = DependencyProperty.Register("IsKOLLManage",
        typeof(bool), typeof(UIVAK),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsKOLLManageChanged));
        public bool IsKOLLManage
        {
            get
            {
                return (bool)GetValue(IsKOLLManageProperty);
            }
            set
            {
                SetValue(IsKOLLManageProperty, value);
            }
        }
        private static void OnIsKOLLManageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVAK;
            if (src != null)
            {
                src.IsKOLLManage = (bool)e.NewValue;
                src.OnIsKOLLManageChg((bool)e.NewValue);
            }
        }
        public virtual void OnIsKOLLManageChg(bool value)
        {

        }
        #endregion



        public UIVAK()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);

        }
        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            switch (app.company)
            {
                case "TST":
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            KM1 = "TSK".PadRight(5);
                            break;
                        case "TMODE":
                            KM1 = "TSM".PadRight(5);
                            break;

                    }
                    break;
                case "YGR":
                case "JLT":
                    KM1 = "".PadRight(5);
                    break;
                default:
                    KM1 = app.company.PadRight(5);
                    break;
            }
        }

        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.DeleteCommand(sender, e);
            if (dwMaster == null)
                return;
            ((VVAKOLL)MV).DeleteVAKOLL(dwMaster, 1);
            string VAKKOLL = dwMaster["VAKKOLL"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            string filter = "VAKKOLL='" + VAKKOLL + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KOLL.Trim().Length == 0)
            {
                MessageBox.Show("请先输入新季节");
                return;
            }
            if (dwMaster == null)
                return;
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["VAKKOLL"] = KOLL;
            dwData["VAKAENDBEN"] = app.User;
            dwData["VAKAENDDAT"] = MV.date;
            ((VVAKOLL)MV).Insert(dwData);
            base.CopyCommand(sender, e);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void Save()
        {
            base.Save();
            if (dwMaster == null)
                return;
            dwMaster["VAKDATVON"] = MV.date;
            ((VVAKOLL)MV).UpdateVAKOLL(dwMaster, 0);
        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                Save();
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
